##数据库封装
import pymysql
class DButils:
    def __init__(self, host="localhost", port="3306", user="root", passwd="root", db="t_mysql"):
        self.__host = host
        self.__port = port
        self.__user = user
        self.__passwd= passwd
        self.__db = db

    def update(self,sql, params):
        conn=None
        try:
            conn=pymysql.connect(host=self.__host,
                                 port=int(self.__port),
                                 user=self.__user,
                                 password=self.__passwd,
                                 database=self.__db)
            with conn.cursor() as cursor:
                cursor.execute(sql, params)
                conn.commit()
        except Exception as e:
            print(e)
        finally:
            if conn:
                conn.close()

    def select(self,sql,params=None):
        conn=None
        try:
            conn = pymysql.connect(host=self.__host,
                                   port=int(self.__port),
                                   user=self.__user,
                                   password=self.__passwd,
                                   database=self.__db)
            with conn.cursor() as cursor:
                cursor.execute(sql, params)
                r=cursor.fetchall()
                for i in r:
                    print(i)
        except Exception as e:
            print(e)
        finally:
            if conn:
                conn.close()


